package cn.fyl.order.service.impl;

import cn.fyl.basic.RocketMq.RocketMQProducer;
import cn.fyl.basic.util.PageList;
import cn.fyl.order.domain.OrderPetAcquisition;
import cn.fyl.order.dto.ConfirmDto;
import cn.fyl.order.mapper.OrderPetAcquisitionMapper;
import cn.fyl.order.query.OrderPetAcquisitionQuery;
import cn.fyl.order.service.IOrderPetAcquisitionService;
import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.org.domain.Employee;
import cn.fyl.org.domain.Shop;
import cn.fyl.org.mapper.EmployeeMapper;
import cn.fyl.org.mapper.ShopMapper;
import cn.fyl.pet.domain.Pet;
import cn.fyl.pet.mapper.PetMapper;
import cn.fyl.pet.mapper.SearchMasterMsgMapper;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;



    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, Long id) {

        Employee employee = employeeMapper.loadByLogininfoId(id);

        if(employee.getShop_id()!=null){
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdmin_id()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());
        }
        return super.queryData(query);
    }

    @Override
    public void confirm(ConfirmDto dto) {
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();

        // 修改订单状态，金额，支付方式
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadById(orderId);
        opa.setState(2);
        opa.setPrice(money);
        opa.setPaytype(payType);
        orderPetAcquisitionMapper.update(opa);


        // 修改寻主消息状态
        searchMasterMsgMapper.updateState(opa.getSearchMasterMsgId());

        // 修改pet 金额
        Pet pet = petMapper.loadById(opa.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);




    }

    @Override
    public void cancel(Long id) {
        /**
         * 1. 修改订单状态
         * 2. 寻主消息的shopid=null
         * 3. pet 删除
         */
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadById(id);
        opa.setState(-1);
        orderPetAcquisitionMapper.update(opa);

        searchMasterMsgMapper.reject(opa.getShopId());

        petMapper.remove(opa.getPetId());
    }

    @Override
    public OrderPetAcquisition loadBySn(String msg) {
        return orderPetAcquisitionMapper.loadBySn(msg);
    }


}
